/*
 * Copyright (c) Scrap Material Junior Brother, Xin. 2023-2023. All rights reserved.
 */

package collection.sort;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 使用Comparator方式来自定义排序
 *
 * @author xin
 * @since 2023-05-02
 */
public class Comparators {
    public static void main(String[] args) {
        int[] arr = {2, -1, 3, 0, 4, -8};
        List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
        list.sort(Comparator.reverseOrder());
        System.out.println(list);

        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Tom", 12));
        persons.add(new Person("John", 18));
        persons.add(new Person("Jane", 10));
        System.out.println(persons);
        persons.sort(Comparator.comparing(o -> o.age));
        System.out.println(persons);

        List<ComparablePerson> comparablePersons = new ArrayList<>();
        comparablePersons.add(new ComparablePerson("Tony", 13));
        comparablePersons.add(new ComparablePerson("July", 16));
        comparablePersons.add(new ComparablePerson("Mak", 11));
        comparablePersons.add(new ComparablePerson("Jack", 11));
        Collections.sort(comparablePersons);
        System.out.println(comparablePersons);

        // 使用set需要重写equal和hashCode
        Set<ComparablePerson> personSet = new TreeSet<>(comparablePersons);
        System.out.println(personSet);

        PriorityQueue<ComparablePerson> personQueue = new PriorityQueue<>(comparablePersons);
        System.out.println(personQueue);
    }
}
